import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { CopyLeaveBtnView } from "../view/common/CopyLeaveBtnView";
import { BossfieldIconView } from "../view/bossField/BossfieldIconView";
import { BossFieldTopWindow } from "../view/bossField/BossFieldTopWindow";
import { BossFieldMiniScoreView } from "../view/bossField/BossFieldMiniScoreView";
import { SBossFieldOpen } from "../../../Message/Public/SBossFieldOpen";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { ClockManager } from "../manager/ClockManager";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { BossFieldEntranceWindow } from "../view/bossField/BossFieldEntranceWindow";
import { DataEvent } from "../events/DataEvent";
import { SystemSetter } from "../view/system/SystemSetter";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { BossFieldCliffordView } from "../view/bossField/BossFieldCliffordView";
import { SNewBattleFieldInspireInfo } from "../../../Message/Public/SNewBattleFieldInspireInfo";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { GameProxy } from "../mvc/GameProxy";
import { SBossFieldList } from "../../../Message/Public/SBossFieldList";
import { SCrossBossFieldList } from "../../../Message/Public/SCrossBossFieldList";
import { SBossFieldScore } from "../../../Message/Public/SBossFieldScore";
import { ECamp } from "../../../Message/Public/ECamp";
import { SBossFieldForceScore } from "../../../Message/Public/SBossFieldForceScore";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
type int = number;
//class BossFieldController
    
    export  class BossFieldController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BossFieldOpen, this.onBossFieldOpen);
            NetDispatcher.addCmdListener(ServerCommand.BossFieldClose, this.onBossFieldClose);
            NetDispatcher.addCmdListener(ServerCommand.BossFieldEnter, this.onBossFieldEnter);
            NetDispatcher.addCmdListener(ServerCommand.BossFieldLeave, this.onBossFieldLeave);
            NetDispatcher.addCmdListener(ServerCommand.BossFieldListUpdate, this.onBossFieldList);
            NetDispatcher.addCmdListener(ServerCommand.BossFieldPlayerScoresUpdate, this.onBossFieldPlayerScoresUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BossFieldPlayerCreditUpdate, this.onBossFieldPlayerCreditUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BossFieldForceCreditUpdate, this.onBossFieldForceCreditUpdate);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BossFieldInspireInfoUpdate, this.onInspireInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ActiveAddBattleField, this.onActiveBossFieldHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicOpenCrossBossField, this.onCrossBossFieldOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCloseCrossBossField, this.onBossFieldClose);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossBossFieldList, this.onCrossBossFieldList);
            Dispatcher.addEventListener(EventName.BossFieldShowIntroduce, this.onBossFieldShowIntroduce);
            Dispatcher.addEventListener(EventName.BossFieldRequestList, this.onBossFieldRequestList);
            Dispatcher.addEventListener(EventName.BossFieldRequestComein, this.onBossFieldRequestComein);
            Dispatcher.addEventListener(EventName.BossFieldRequestResult, this.onBossFieldRequestResult);
            Dispatcher.addEventListener(EventName.BossFieldRequestLeave, this.onBossFieldRequestLeave);
            Dispatcher.addEventListener(EventName.BossFieldRequestLeaveAffirm, this.onBossFieldRequestLeaveAffirm);
            Dispatcher.addEventListener(EventName.BossFieldClifford, this.onBossFieldClifford);
            Dispatcher.addEventListener(EventName.BossFieldGet, this.onBossFieldGet);
            Dispatcher.addEventListener(EventName.SysSettingSuccess, this.onSettingChange);
            Dispatcher.addEventListener(EventName.BossFieldBossRevive, this.onBossFieldRevive);
            Dispatcher.addEventListener(EventName.CrossBossFieldRequestList, this.onCrossBossFieldRequestList);
            Dispatcher.addEventListener(EventName.CrossBossFieldRequestComein, this.onCrossBossFieldRequestComein);
            return;
        }

        /* internal  */get leaveBtnView(): CopyLeaveBtnView
        {
            if (!this._leaveBtnView) 
            {
                this._leaveBtnView = new CopyLeaveBtnView(this.onBossFieldRequestLeave);
            }
            return this._leaveBtnView;
        }

        public get icon(): BossfieldIconView
        {
            if (!this._icon) 
            {
                this._icon = new BossfieldIconView();
            }
            return this._icon;
        }

        public get topWindow(): BossFieldTopWindow
        {
            return BossFieldTopWindow.instance;
        }

        /* internal  */onBossFieldClose(arg1: Object): void
        {
            this._isCloseBossField = true;
            if (this._icon) 
            {
                this._icon.hide();
            }
            BossFieldTopWindow.instance.showLeaveBtn();
            cache.bossField.isCrossBossField = false;
            return;
        }

        public get miniScoreView(): BossFieldMiniScoreView
        {
            if (!this._miniScoreView) 
            {
                this._miniScoreView = new BossFieldMiniScoreView();
            }
            return this._miniScoreView;
        }

        /* internal  */onBossFieldOpen(arg1: SBossFieldOpen): void
        {
            cache.bossField.isCrossBossField = false;
            this.onBossFieldOpenHandler(arg1);
            return;
        }

        /* internal  */onCrossBossFieldOpen(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SBossFieldOpen;
            cache.bossField.isCrossBossField = true;
            this.onBossFieldOpenHandler(loc1);
            return;
        }

        /* internal  */onBossFieldOpenHandler(arg1: SBossFieldOpen): void
        {
            this._isCloseBossField = false;
            this._bossFieldOpen = arg1;
            var loc1=ClockManager.instance.nowDate.time - this._bossFieldOpen.openDt.time < 5000;
            var loc2=ClockManager.instance.nowDate.time - this._bossFieldOpen.openDt.time < -2000;
            this.icon.show();
            if (!loc1 && cache.role.entityInfo.level >= LevelLimitDefin.BossFieldMinLevel) 
            {
                this.entranceWindow.show();
            }
            if (!loc2 && cache.role.entityInfo.level >= LevelLimitDefin.BossFieldMinLevel) 
            {
                this.icon.showLightMask();
            }
            else 
            {
                this.icon.hideLightMask();
            }
            if (loc2) 
            {
                this.icon.startCountdownLimitEnter(this._bossFieldOpen.openDt);
            }
            return;
        }

        public get entranceWindow(): BossFieldEntranceWindow
        {
            return BossFieldEntranceWindow.instance;
        }

        /* internal  */onBossFieldEnter(arg1: Object): void
        {
            this._isInBossField = true;
            this.miniScoreView.updateTfCamp();
            this.miniScoreView.show();
            this.leaveBtnView.show();
            if (BossFieldEntranceWindow.hasInstance()) 
            {
                BossFieldEntranceWindow.instance.hide();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyEnterCopyMap));
            this.battlefieldCountDownUpdate(this._bossFieldOpen.endDt);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
            this.lastSettingHideLifeMagic = SystemSetter.currentSetter.hideLifeMagic;
            SystemSetter.currentSetter.hideLifeMagic = false;
            ThingUtil.entityUtil.updateLifeShow();
            return;
        }

        /* internal  */onBossFieldLeave(arg1: Object): void
        {
            this._isInBossField = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveCopyMap));
            if (this._miniScoreView) 
            {
                this.miniScoreView.hide();
            }
            if (this._leaveBtnView) 
            {
                this._leaveBtnView.hide();
            }
            if (BossFieldCliffordView.hasInstance()) 
            {
                BossFieldCliffordView.instance.hide();
            }
            if (this.lastSettingHideLifeMagic && !this.isChangeAtMiddle) 
            {
                SystemSetter.currentSetter.hideLifeMagic = true;
                ThingUtil.entityUtil.updateLifeShow();
            }
            if (BossFieldTopWindow.hasInstance()) 
            {
                BossFieldTopWindow.instance.hide();
            }
            if (BossFieldEntranceWindow.hasInstance()) 
            {
                BossFieldEntranceWindow.instance.hide();
            }
            return;
        }

        /* internal  */onMoneyUpdate(arg1: Object): void
        {
            if (BossFieldEntranceWindow.hasInstance()) 
            {
                BossFieldEntranceWindow.instance.updateMyHonor(cache.role.money.honour);
            }
            return;
        }

        /* internal  */onInspireInfoUpdate(arg1: SNewBattleFieldInspireInfo): void
        {
            if (this._miniScoreView) 
            {
                this._miniScoreView.updateInspireInfo(arg1);
            }
            if (BossFieldCliffordView.hasInstance()) 
            {
                BossFieldCliffordView.instance.updateInspire(arg1);
            }
            return;
        }

        /* internal  */onActiveBossFieldHandler(arg1: SActiveToClient): void
        {
            if (this._icon && !this._icon.isHide) 
            {
                this._icon.updateActiveToClientInfo(arg1);
            }
            return;
        }

        /* internal  */onBossFieldRevive(arg1: DataEvent): void
        {
            GameProxy.copy.moveToPassPoint(0);
            return;
        }

        /* internal  */onSettingChange(arg1: DataEvent): void
        {
            this.isChangeAtMiddle = true;
            return;
        }

        /* internal  */battlefieldCountDownUpdate(arg1: Date): void
        {
            this.miniScoreView.startCountdown(arg1);
            return;
        }

        /* internal  */onBossFieldList(arg1: SBossFieldList): void
        {
            BossFieldEntranceWindow.instance.updateBossFieldList(arg1.fields);
            return;
        }

        /* internal  */onCrossBossFieldList(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCrossBossFieldList;
            BossFieldEntranceWindow.instance.updateBossFieldList(loc1.fields, loc1.force);
            return;
        }

        /* internal  */onBossFieldPlayerScoresUpdate(arg1: Object): void
        {
            BossFieldTopWindow.instance.refreshScores(cache.bossField.vccampPlayerScoreAll);
            return;
        }

        /* internal  */onBossFieldPlayerCreditUpdate(arg1: Object): void
        {
            this.miniScoreView.updateMyScore(cache.bossField.bossFieldMyScore);
            return;
        }

        /* internal  */onBossFieldForceCreditUpdate(arg1: SBossFieldScore): void
        {
            var loc1=null;
            var loc2=(arg1.scores[ECamp._ECampOne] as SBossFieldForceScore).score;
            var loc3=(arg1.scores[ECamp._ECampTwo] as SBossFieldForceScore).score;
            var loc4=(arg1.scores[ECamp._ECampThree] as SBossFieldForceScore).score;
            this.miniScoreView.updateForceScore(loc2, loc3, loc4);
            return;
        }

        /* internal  */onBossFieldShowIntroduce(arg1: DataEvent): void
        {
            BossFieldEntranceWindow.instance.show();
            return;
        }

        /* internal  */onBossFieldRequestList(arg1: DataEvent): void
        {
            GameProxy.battlefieldProxy.getBossFieldInfo();
            return;
        }

        /* internal  */onCrossBossFieldRequestList(arg1: DataEvent): void
        {
            GameProxy.battlefieldProxy.getCrossBossFieldInfo();
            return;
        }

        /* internal  */onBossFieldRequestComein(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=0;
            var loc1=arg1.data;
            loc2 = loc1["fieldId"] as SEntityId;
            loc3 = loc1["force"] as int;
            GameProxy.battlefieldProxy.enterBossField(loc2, loc3);
            return;
        }

        /* internal  */onCrossBossFieldRequestComein(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=0;
            var loc1=arg1.data;
            loc2 = loc1["fieldId"] as SEntityId;
            loc3 = loc1["force"] as int;
            GameProxy.battlefieldProxy.enterCrossBossField(loc2, loc3);
            return;
        }

        /* internal  */onBossFieldRequestResult(arg1: DataEvent): void
        {
            GameProxy.battlefieldProxy.getBossFieldScoreList();
            return;
        }

        /* internal  */onBossFieldRequestLeave(arg1: DataEvent=null): void
        {
            Alert.show(Language.getString(44700), null, Alert.OK | Alert.CANCEL, null, this.onSelectLeave);
            return;
        }

        /* internal  */onSelectLeave(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                GameProxy.battlefieldProxy.leftBossField();
            }
            return;
        }

        /* internal  */onBossFieldRequestLeaveAffirm(arg1: DataEvent): void
        {
            GameProxy.battlefieldProxy.leftBossField();
            return;
        }

        /* internal  */onBossFieldClifford(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            GameProxy.battlefieldProxy.inspireInBossField(loc1);
            return;
        }

        /* internal  */onBossFieldGet(arg1: DataEvent): void
        {
            GameProxy.battlefieldProxy.submitBossFieldTask();
            return;
        }

        private /* var */_bossFieldOpen: SBossFieldOpen;

        private /* var */_isInBossField: Boolean=false;

        private /* var */lastSettingHideLifeMagic: Boolean=false;

        private /* var */_isCloseBossField: Boolean=true;

        private /* var */isChangeAtMiddle: Boolean=false;

        private /* var */_miniScoreView: BossFieldMiniScoreView;

        private /* var */_icon: BossfieldIconView;

        private /* var */_leaveBtnView: CopyLeaveBtnView;
    }
